package coml.jiyun.hp.reviewtopic.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.ArrayList;
import coml.jiyun.hp.reviewtopic.R;
import coml.jiyun.hp.reviewtopic.ui.adapter.MainAdapter;
import coml.jiyun.hp.reviewtopic.ui.fragment.CollectFragment;
import coml.jiyun.hp.reviewtopic.ui.fragment.HomeFragment;
import coml.jiyun.hp.reviewtopic.ui.fragment.MyFragment;

public class HomeActivity extends AppCompatActivity {

    /**
     * 首页
     */
    private TextView mToolTv;
    private Toolbar mHomeTool;
    private ViewPager mHomeVp;
    private TabLayout mHomeTab;
    private DrawerLayout mD1;
    private ArrayList<Fragment> fragments;
    private NavigationView mNa;
    private LinearLayout mLl;
    private ImageView viewById;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        initRes();
    }

    private void initRes() {
        String[] res = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.READ_CALENDAR,
                Manifest.permission.WRITE_CALENDAR
        };
    }

    private void initView() {
        mToolTv = (TextView) findViewById(R.id.tool_tv);
        mHomeTool = (Toolbar) findViewById(R.id.home_tool);
        mHomeVp = (ViewPager) findViewById(R.id.home_vp);
        mHomeTab = (TabLayout) findViewById(R.id.home_tab);
        mD1 = (DrawerLayout) findViewById(R.id.d1);
        mNa = (NavigationView) findViewById(R.id.na);

        mHomeTool.setTitle("");
        setSupportActionBar(mHomeTool);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mD1, mHomeTool, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.syncState();

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new CollectFragment());
        fragments.add(new MyFragment());

        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragments);
        mHomeVp.setAdapter(mainAdapter);
        mHomeTab.setupWithViewPager(mHomeVp);

        mHomeTab.getTabAt(0).setText("首页");
        mHomeTab.getTabAt(1).setText("收藏");
        mHomeTab.getTabAt(2).setText("我的");
        mHomeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int selectedTabPosition = mHomeTab.getSelectedTabPosition();
                if (selectedTabPosition == 0) {
                    mToolTv.setText("首页");
                } else if (selectedTabPosition == 1) {
                    mToolTv.setText("收藏");
                } else {
                    mToolTv.setText("我的");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        View headerView = mNa.getHeaderView(0);
        viewById = headerView.findViewById(R.id.thread_img);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popwindow();
            }
        });

        mNa.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.xiazai:
                        startActivity(new Intent(HomeActivity.this, DownActivity.class));
                        break;

                    case R.id.daohang:
                        startActivity(new Intent(HomeActivity.this, NaviActivity.class));
                        break;

                    case R.id.chakantupian:
                        startActivity(new Intent(HomeActivity.this, PhotoActivity.class));
                }
                return false;
            }
        });
        mLl = (LinearLayout) findViewById(R.id.ll);
    }

    private void popwindow() {

        View inflate = LayoutInflater.from(this).inflate(R.layout.popwindow_img, null);
        inflate.findViewById(R.id.pop_kaiqixiangji).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);
            }
        });

        inflate.findViewById(R.id.pop_kaiqixiangce).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenXiangCe();

            }
        });

        PopupWindow popupWindow = new PopupWindow(inflate, GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.WRAP_CONTENT);
        TextView viewById = inflate.findViewById(R.id.pop_kaiqixiangji);
        TextView viewById2 = inflate.findViewById(R.id.pop_kaiqixiangce);
        viewById.setText("开启相机");
        viewById2.setText("开启相册");
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(mLl, Gravity.CENTER, 0, 0);
    }

    private void OpenXiangCe() {
        //打开相册
        Intent intent = new Intent(Intent.ACTION_PICK,
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 200:
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    // 通过uri的方式返回，设置到图片组件上
                    viewById.setImageURI(uri);
                    System.out.println("----------------uri--------------");
                }
                break;

            case 100:

                if (resultCode == RESULT_OK){
                    Bitmap data1 = (Bitmap) data.getExtras().get("data");
                    viewById.setImageBitmap(data1);
                }
                break;
        }
    }

}